package com.tuyrt.windy.ui.place

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.tuyrt.windy.logic.Repository
import com.tuyrt.windy.logic.model.Place

/**
 * Created by tuyrt7 on 2021/4/14.
 */
class PlaceViewModel : ViewModel() {

    private val searchLiveData = MutableLiveData<String>()

    val placeList = ArrayList<Place>()

    //提供给 UI 层观察的数据源
    val placeLiveData = Transformations.switchMap(searchLiveData) { query ->
        Repository.searchPlaces(query)
    }

    //提供给 UI 层刷新数据的接口
    fun searchPlace(query: String) {
        searchLiveData.value = query
    }

    fun savePlace(place: Place) = Repository.savePlace(place)
    fun getPlace() = Repository.getPlace()
    fun isPlaceSaved() = Repository.isPlaceSaved()
}